<?php
/*
  导入房间号
*/
namespace app\commands;

use Yii;
use yii\console\Controller;
use app\modules\cruise\models\RoomCode;


class RoomController extends Controller
{

	//导入房间号
	public function actionImport(){//{{{
		$path = sprintf('%s/web/room6.xlsx', Yii::$app->basePath);
		
        $datas = $this->getData($path);

        $types = [
                1 => '内舱',
                2 => '海景',
                3 => '海景阳台',
                4 => '套房',
        ];

        foreach($datas as $data){
        	if($data){
        		foreach($data as $key=>$val){
        			
        			if($val['A'] && $val['B'] && $val['C']){
        				$roomcode = new RoomCode();
        				$roomcode->cruise_id = $val['A'];
        				$roomcode->deck = $val['B'];
        				$roomcode->code = $val['C'];
        				$roomcode->type = array_search($val['D'],$types);
        				$roomcode->type_code = $val['E'];
        				$roomcode->link_room = $val['F'];

        				$roomcode->location = $val['G'];
        				$roomcode->color = $val['H'];
        				$roomcode->created_at = date("Y-m-d H:i:s");
        				if($roomcode->save()){
        					echo "success ---".$roomcode->id."----\n";
        				}

        			}
        		}
        	}
        }
        
	
	}//}}}


	//获取excel 数据
	private function getData($filePath){//{{{
		if(empty($filePath)){
			return [];
		}
		$PHPReader = new \PHPExcel_Reader_Excel2007();

		if(!$PHPReader->canRead($filePath)){
		    $PHPReader = new \PHPExcel_Reader_Excel5();
		}

		if(!$PHPReader->canRead($filePath)){
		    return ;
		}

		$PHPExcel = $PHPReader->load($filePath);

		$sheetCount = $PHPExcel->getSheetCount();

        $data = [];
        for($i=0; $i<$sheetCount; $i++){

		    $currentSheet = $PHPExcel->getSheet($i);

			/**取得最大的列号*/
		 	$allColumn = $currentSheet->getHighestColumn();

			/**取得一共有多少行*/
			$allRow = $currentSheet->getHighestRow();
						
			for($rowIndex=2;$rowIndex<=$allRow;$rowIndex++){
			    //for($colIndex='A';$colIndex<=$allColumn;$colIndex++){
			    for($colIndex='A';$colIndex<='V';$colIndex++){
			        $addr = $colIndex.$rowIndex;
			        $cell = $currentSheet->getCell($addr)->getValue();
			        if($cell instanceof PHPExcel_RichText){     //富文本转换字符串
			            $cell = $cell->__toString();
					}
					if(!empty($cell)){
			        	$arrExcel[$rowIndex][$colIndex]= trim($cell);
					}else{
						$arrExcel[$rowIndex][$colIndex]= '';
					}
					$cell = '';
			    }

			}

            $data[$i] = $arrExcel;  
            $arrExcel = [];
	    }		
		return $data;
	}//}}}

    //导入最大入住人数
    public function actionNum(){
        $path = sprintf('%s/web/num3.xlsx', Yii::$app->basePath);
        
        $datas = $this->getData2($path);

        foreach($datas as $val){
            if($val){
                $model = RoomCode::find()->where(['cruise_id'=>$val['A'],'code'=>$val['B'] ])->one();
                if($model){
                    $model->bed = $val['C'];
                    if($model->save()){
                        echo "success ---".$model->id."----\n";
                    }
                }else{
                    echo "no room code \n";
                }

            }
        }
       
    }

    //获取excel 数据
    private function getData2($filePath){//{{{
        if(empty($filePath)){
            return [];
        }
        $PHPReader = new \PHPExcel_Reader_Excel2007();

        if(!$PHPReader->canRead($filePath)){
            $PHPReader = new \PHPExcel_Reader_Excel5();
        }

        if(!$PHPReader->canRead($filePath)){
            return ;
        }

        $PHPExcel = $PHPReader->load($filePath);

        $arrExcel = [];

        $currentSheet = $PHPExcel->getSheet(0);

        /**取得最大的列号*/
        $allColumn = $currentSheet->getHighestColumn();

        /**取得一共有多少行*/
        $allRow = $currentSheet->getHighestRow();
                        
        for($rowIndex=2;$rowIndex<=$allRow;$rowIndex++){
            //for($colIndex='A';$colIndex<=$allColumn;$colIndex++){
            for($colIndex='A';$colIndex<='C';$colIndex++){
                $addr = $colIndex.$rowIndex;
                $cell = $currentSheet->getCell($addr)->getValue();
                if($cell instanceof PHPExcel_RichText){     //富文本转换字符串
                    $cell = $cell->__toString();
                }
                if(!empty($cell)){
                    $arrExcel[$rowIndex][$colIndex]= trim($cell);
                }else{
                    $arrExcel[$rowIndex][$colIndex]= '';
                }
                $cell = '';
            }

        }
   
        return $arrExcel;
    }//}}}

}